/**
* @Title: PointPageView.java
* @date 2011-7-25 12:51:23
* @version V1.0 
 */
package com.founder.android.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class PointPageView extends View {
	
	private int mPageSize;
	private int mPageIndex;
	private int mPointSize;
	private int mPointSpan;
	private int mSelectPointSize;
	private int mStep;
	private int mDisplaySize;
	private int mDisplayIndex;
	private Paint paint;
	
	private void init() {
		mPointSize = 3;
		mSelectPointSize = mPointSize;
		mPointSpan = 3 * mPointSize;
		mStep = 1;
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(Color.WHITE);
	}
	
	public PointPageView(Context context) {
		super(context, null);
		init();
	}
	/**
	 * Used to inflate the Workspace from XML.
	 */
	public PointPageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	public void setPageSize(int pageSize) {
		mPageSize = Math.max(pageSize, 0);
		mDisplaySize = (int)Math.ceil((double)mPageSize / mStep);
		invalidate();
	}
	
	public int getPageSize() {
		return mPageSize;
	}
	
	public void setPageIndex(int pageIndex) {
		mPageIndex = Math.min(Math.max(pageIndex, 0), mPageSize - 1);
		mDisplayIndex = (int)Math.floor((double)mPageIndex / mStep);
		invalidate();
	}
	
	public int getPageIndex() {
		return mPageIndex;
	}
	
	public void setColor(int color) {
		paint.setColor(color);
	}
	
	public void setPointSize(int size) {
		mPointSize = size;
	}
	
	public void setSelectPointSize(int size) {
		mSelectPointSize = size;
	}
	
	public void setPointSpan(int span) {
		mPointSpan = span;
	}
	
	public void setStep(int step) {
		mStep = step;
	}
	
	public int getStep() {
		return mStep;
	}
	
	@Override
	public void draw(Canvas canvas) {
		super.draw(canvas);
		drawAllPoint(canvas);
	}
	
	private void drawAllPoint(Canvas canvas) {
		canvas.save();
		
		final int paddingLeft = mPointSpan;
		final int width = getMeasuredWidth();
		final int height = getMeasuredHeight();
		
		int radius = mPointSize;
		int contentWidth = (radius + paddingLeft) * mPageSize;
		int beginX = (width - contentWidth) / 2;
		int beginY = height / 2;

		for (int i = 0; i < mDisplaySize; i++) {
			if (i == mDisplayIndex) {
				paint.setAlpha(255);
				radius = mSelectPointSize;
			} else {
				paint.setAlpha(128);
				radius = mPointSize;
			}
			canvas.drawCircle(beginX, beginY, radius, paint);
			beginX = beginX + radius + radius + paddingLeft;
		}
		
		canvas.restore();
	}
}